home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / tests / t18.c < prev   
C/C++ Source or Header  |  1994-08-24  |  2KB  |  80 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define TESTFILE "testdata.tmp"
  5.  
  6. static unsigned char data[5000];
  7. static unsigned char buf[5000];
  8.  
  9. static int start, length, type, addl;
  10. static int curpos, seek_amt;
  11.  
  12. void
  13. setup_file(void)
  14. {
  15.   int i;
  16.   FILE *f;
  17.   srand(time(0));
  18.   for (i=0; i<5000; i++)
  19.     data[i] = rand();
  20.   f = fopen(TESTFILE, "wb");
  21.   fwrite(data, 5000, 1, f);
  22.   fclose(f);
  23. }
  24.  
  25. char *tn[] = { "SET", "CUR", "END" };
  26.  
  27. int
  28. main()
  29. {
  30.   FILE *f;
  31.   int loops = 0, i, j;
  32.  
  33.   setup_file();
  34.   f = fopen(TESTFILE, "rb");
  35.   curpos = 0;
  36.   for (i=0; i<1000; i++)
  37.   {
  38.     start = rand() % 5000;
  39.     length = rand() % (5000 - start);
  40.     addl = rand() % 5000;
  41.     switch (rand() % 3)
  42.     {
  43.     case 0:
  44.       type = SEEK_SET;
  45.       seek_amt = start;
  46.       break;
  47.     case 1:
  48.       type = SEEK_CUR;
  49.       seek_amt = start - curpos;
  50.       break;
  51.     case 2:
  52.       type = SEEK_END;
  53.       seek_amt = start - 5000;
  54.       break;
  55.     }
  56.  
  57.     printf("%4d: %4d len %4d, %s\r", loops, start, length, tn[type]);
  58.     fflush(stdout);
  59.     fseek(f, seek_amt, type);
  60.     fread(buf, length, 1, f);
  61.     if (memcmp(data+start, buf, length))
  62.     {
  63.       printf("Error: data read error, start=%d, length=%d, type=%s, addl=%d, curpos=%d, loops=%d\n",
  64.          start, length, tn[type], addl, curpos, loops);
  65.       for (j=0; j<10; j++)
  66.     printf("  %02x:%02x", data[start+j], buf[j]);
  67.       printf("\n");
  68.       break;
  69.     }
  70.     fread(buf, addl, 1, f);
  71.     curpos = start + length + addl;
  72.     if (curpos > 5000)
  73.       curpos = 5000;
  74.     loops++;
  75.   }
  76.   fclose(f);
  77.   remove(TESTFILE);
  78.   return 0;
  79. }
  80.